﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hydra.Shell;

namespace Hydra.Shell.Attributes
{
    [AttributeUsage(System.AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public sealed class ProvideEditorLogicalViewAttribute : RegistrationAttribute
    {
        private System.Guid factory;
        private bool isTrusted = true;
        private System.Guid logicalView;

        public ProvideEditorLogicalViewAttribute(object factoryType, Guid logicalViewGuid) : this(factoryType, logicalViewGuid.ToString())
        {
        }

        public ProvideEditorLogicalViewAttribute(object factoryType, string logicalViewGuid)
        {
            this.logicalView = new System.Guid(logicalViewGuid);
            if (factoryType is string)
            {
                this.factory = new System.Guid((string)((string)factoryType));
            }
            else if (factoryType is System.Type)
            {
                this.factory = ((System.Type)factoryType).GetGuid();
            }
            else
            {
                if (!(factoryType is System.Guid))
                {
                    throw new System.ArgumentException("InvalidFactoryType");
                }
                this.factory = (System.Guid)factoryType;
            }
        }

        public override void Register(RegistrationAttribute.RegistrationContext context)
        {
            using (RegistrationAttribute.Key key = context.CreateKey(this.LogicalViewPath))
            {
                key.SetValue(this.logicalView.ToString("B"), "");
            }
            if (!this.IsTrusted)
            {
                using (RegistrationAttribute.Key key2 = context.CreateKey(this.UntrustedViewsPath))
                {
                    key2.SetValue(this.logicalView.ToString("B"), "");
                }
                using (RegistrationAttribute.Key key3 = context.CreateKey(this.EditorPath))
                {
                    key3.SetValue("EditorTrustLevel", 2);
                }
            }
        }

        public override void Unregister(RegistrationAttribute.RegistrationContext context)
        {
            context.RemoveValue(this.LogicalViewPath, this.logicalView.ToString("B"));
        }

        private string EditorPath
        {
            get
            {
                return string.Format(System.Globalization.CultureInfo.InvariantCulture, @"Editors\{0}", new object[] { this.factory.ToString("B") });
            }
        }

        public System.Guid FactoryType
        {
            get
            {
                return this.factory;
            }
        }

        public bool IsTrusted
        {
            get
            {
                return this.isTrusted;
            }
            set
            {
                this.isTrusted = value;
            }
        }

        public System.Guid LogicalView
        {
            get
            {
                return this.logicalView;
            }
        }

        private string LogicalViewPath
        {
            get
            {
                return string.Format(System.Globalization.CultureInfo.InvariantCulture, @"{0}\LogicalViews", new object[] { this.EditorPath });
            }
        }

        private string UntrustedViewsPath
        {
            get
            {
                return string.Format(System.Globalization.CultureInfo.InvariantCulture, @"{0}\UntrustedLogicalViews", new object[] { this.EditorPath });
            }
        }
    }
}
